﻿using System;
using System.Linq;

namespace Susam
{
    class AttributeTable
    {
        protected IQueryable<System.Data.DataRow> jncTable;
        protected IQueryable<System.Data.DataRow> table;

        public String GetAttributeListforBookAsString(int BookID)
        {
            String ret = "";

            var names =
                from tblRow in table
                join jncRow in jncTable on ((int)tblRow[0]) equals ((int)jncRow[1])
                where (int)jncRow[0] == BookID
                select (String)tblRow[1];

            foreach (var name in names)
            {
                if (ret.Count() > 0)
                {
                    ret = ret + "; ";
                }
                ret = ret + name;
            }

            return ret;
        }
    }

    class AuthorAttribute : AttributeTable
    {
        public AuthorAttribute()
        {
            this.table = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.tblPerson.AsQueryable<System.Data.DataRow>();
            this.jncTable = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.jncBookAuthor.AsQueryable<System.Data.DataRow>();
        }
    }

    class LanguageAttribute : AttributeTable
    {
        public LanguageAttribute()
        {
            this.table = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.tblLanguage.AsQueryable<System.Data.DataRow>();
            this.jncTable = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.jncBookLanguage.AsQueryable<System.Data.DataRow>();
        }
    }

    class PublisherAttribute : AttributeTable
    {
        public PublisherAttribute()
        {
            this.table = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.tblPublisher.AsQueryable<System.Data.DataRow>();
            this.jncTable = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.jncBookPublisher.AsQueryable<System.Data.DataRow>();
        }
    }

    class FileTypeAttribute : AttributeTable
    {
        public FileTypeAttribute()
        {
            this.table = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.tblFileType.AsQueryable<System.Data.DataRow>();
            this.jncTable = (IQueryable<System.Data.DataRow>)SusamDS.DataSet.jncBookFileType.AsQueryable<System.Data.DataRow>();
        }
    }

    class ContributorAttribute
    {
        class ContributorDisplay
        {
            public String Name;
            public String Role;
        }

        public String GetAttributeListforBookAsString(int BookID)
        {
            String ret = "";

            var contributors =
                from tblRow in SusamDS.DataSet.tblPerson
                join jncRow in SusamDS.DataSet.jncBookContributor on (tblRow.PersonID) equals (jncRow.ContributorID)
                where jncRow.BookID == BookID
                select new ContributorDisplay() { Name = tblRow.PersonName, Role = jncRow.ContributorRole };

            foreach (var contributor in contributors)
            {
                if (ret.Count() > 0)
                {
                    ret = ret + "; ";
                }
                ret = ret + contributor.Name + " [" + contributor.Role + "]";
            }

            return ret;
        }
    }

}
